package day15;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;


/**
 * @author shkstart
 * @creat 2022-05-18:35
 */
public class StudentServer {
    public static final  String FILEPATH = "day15/Students.xml";

    public static void writeToXML(Document doc,String path){
        //...
    }
    /**
     * 获取Document对象
     * @return
     */
    public static Document getDoc(){
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(StudentServer.class.getClassLoader().getResourceAsStream(FILEPATH));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return document;
    }

    /**
     * 获取包含所有的学生对象的集合students
     * @return students集合
     */
    public static List<Student> getAllStu(){
        //获取Document对象
        Document doc = getDoc();
        //获取跟节点
        Element root = doc.getRootElement();
        //获取根节点下的所有元素
        List<Element> elements = root.elements("student");
        //声明Student
        Student student = null;
        //创建ArrayLsit集合存储所有的学生对象
        ArrayList<Student> students = new ArrayList<>();
        //遍历elements集合将xml文档中的数据转换成Student对象并写进students集合中
        for (Element element:elements){
            //创建student对象
            student = new Student();
            student.setName(element.elementTextTrim("name"));
            student.setAge(Integer.parseInt(element.elementTextTrim("age")));
            student.setGender(element.elementTextTrim("gender"));
            student.setClassName(element.elementTextTrim("className"));
            //写进students集合中
            students.add(student);
        }
        return students;
    }

    /**
     *  查询单个学员信息
     * @param name
     * @return 返回符合条件的学生对象student
     */
    public static Student getStu(String name){
        List<Student> students = getAllStu();
        for (Student student:students){
            if(name.equals(student.getName())){
                return student;
            }
        }
        return null;
    }

    /**
     * 程序入口
     * @param args
     */
    public static void main(String[] args) {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(8889);
            System.out.println("服务器启动...");
            while (true){
                new stuThread(ss.accept()).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
class stuThread extends Thread{
    private final Socket socket;

    public stuThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        System.out.println(socket + "成功连接");
        try {
            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while (true){
                int no = is.read();

                switch (no){
                    case 0:{
                        System.out.println(socket+"断开连接！！");
                        return;
                    }
                    case 1:{
                        List<Student> allStu = StudentServer.getAllStu();
                        oos.writeObject(allStu);
                        //对象流的刷新操作
                        oos.reset();
                        //可以不flush
                        //oos.flush();
                        break;
                    }
                    case 2:{
                        //此时在客户端不要忘记加换行
                        String name = br.readLine();
                        Student student = StudentServer.getStu(name);
                        oos.writeObject(student);
                        //对象流的刷新操作
                        oos.reset();
                        break;
                    }
//                    default:{
//                        System.out.println("无效数字！");
//                        break;
//                    }
                }
//                String exit = br.readLine();
//                if("exit".equals(exit)){
//                    System.out.println("退出！");
//                    break;
//                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




    }
}
